package com.study.d030animation;

import javafx.animation.Animation.Status;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import javafx.util.Duration;

/**时间线动画-自定义旋转，缩放
*@description
*@author xtayhame
*date 2020年3月22日
*version
*since v1.0
*/
public class TimelineMain3 extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(20);
		Button b1=new Button("播放");
		Button b2=new Button("暂停");
		Button b3=new Button("停止");
		Button b4=new Button("设置加速");
		Button b7=new Button("从第二个点开始");

		//对rectangle 进行自转和公转  ,如果如果有多个旋转，应先公转，后自转，否则位置会发生变化
		Rectangle rectangle = new Rectangle(100, 100,Paint.valueOf("#EE82EE55"));
		//自定义缩放（公转）
		Rotate rotate1 = new Rotate(0, 200, 200);
		//自定义旋转（自转）
		Rotate rotate2 = new Rotate(0, 0, 100);

		rectangle.getTransforms().addAll(rotate1,rotate2);


		hBox.getChildren().addAll(b1,b2,b3,b4,b7);
		anchorPane.getChildren().addAll(hBox,rectangle);

		AnchorPane.setTopAnchor(rectangle, 100.0);
		AnchorPane.setLeftAnchor(rectangle, 100.0);

		stage.setScene(new Scene(anchorPane, 800, 600));
		stage.show();

		//时间线动画
		Timeline timeline = new Timeline();
		//对矩形进行缩放,旋转
		KeyValue kv1R1 = new KeyValue(rotate1.angleProperty(),0);
		KeyValue kv1R2 = new KeyValue(rotate2.angleProperty(),0);
		KeyFrame kf1 = new KeyFrame(Duration.seconds(0), kv1R1,kv1R2);

		KeyValue kv2R1 = new KeyValue(rotate1.angleProperty(),360);
		KeyValue kv2R2 = new KeyValue(rotate2.angleProperty(),360);
		KeyFrame kf2 = new KeyFrame(Duration.seconds(2), kv2R1,kv2R2);



		timeline.getKeyFrames().addAll(kf1,kf2);
		timeline.setCycleCount(5);
		timeline.setAutoReverse(false);


		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				timeline.play();
			}
		});

	}



}
